/* $Id: cfg.h 16 2006-12-16 15:37:49Z judas $ */

#ifndef NIH_CFG_H
#define NIH_CFG_H 1

#include <nih/cdefs.h>

BEGIN_C_DECLS

#include <nih/htable.h>
#include <nih/list.h>

# include <inttypes.h>
#if HAVE_STDBOOL_H
# include <stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <stdio.h>
#include <sys/types.h>

struct nih_cfg_entry {
        char *name;

        char *short_opt;     /* p */
        char *long_opt;      /* pager */
        char *env_opt;       /* PAGER */
        char *file_opt;      /* PAGER */

        char *default_value; /* less */
        char **value;        /* &opt_pager */
};

typedef int nih_cfg_parse_fnc(struct nih_cfg *cfg, char **name, char **value);

struct nih_cfg {
        struct nih_llist *entries; /* Linked list of nih_cfg_entry's */
        struct nih_htable *lookup; /* Associates names with enries */
        nih_cfg_parse_fnc *parse_fnc;
};

/* Default cfg parser function */
extern int nih_cfg_parse(struct nih_cfg *cfg, char **name, char **value);

extern struct nih_cfg *nih_cfg_create(FILE *cfg_file,
                                      nih_cfg_parse_fnc *parse_fnc);

extern int nih_cfg_add(struct nih_cfg *cfg, struct nih_cfg_entry entry);

extern int nih_cfg_process(struct nih_cfg *cfg,
                           int argc, char *argv[], FILE *file);

extern int nih_cfg_str(struct nih_cfg *cfg, const char *name, char **value);

extern int nih_cfg_dec(struct nih_cfg *cfg, const char *name, int32_t *value);

/* supports decimal, octal and hexadecimal values */
extern int nih_cfg_int(struct nih_cfg *cfg, const char *name, int32_t *value);

extern int nih_cfg_float(struct nih_cfg *cfg, const char *name, float *value);

/* true if has value and value is not "false" or "no" */
extern int nih_cfg_bool(struct nih_cfg *cfg, const char *name, bool *value);

extern int nih_cfg_save(struct nih_cfg *cfg, const char *cfgfile);

extern int nih_cfg_destroy(struct nih_cfg *cfg);

END_C_DECLS

#endif /* NIH_CFG_H */

